package com.swind.fast.modules.app.halali.customer.service.impl;

import com.swind.fast.common.util.oConvertUtils;

import com.swind.fast.modules.app.halali.customer.entity.Cart;
// import com.swind.fast.modules.app.halali.customer.entity.CartBill;
import com.swind.fast.modules.app.halali.customer.mapper.CartMapper;
import com.swind.fast.modules.app.halali.customer.service.ICartService;
// import com.swind.fast.modules.app.halali.customer.model.SpaceAnnouncementModel;

import com.swind.fast.modules.app.halali.customer.entity.Order;
import com.swind.fast.modules.app.halali.customer.service.IOrderService;
import com.swind.fast.modules.app.halali.customer.entity.OrderItem;
import com.swind.fast.modules.app.halali.customer.service.IOrderItemService;

import com.swind.fast.modules.app.halali.customer.entity.OrderItem;
import com.swind.fast.modules.app.halali.customer.entity.RestaurantMeal;
import com.swind.fast.modules.app.halali.customer.service.IRestaurantMealService;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.ApplicationContext;

import org.apache.ibatis.annotations.Param;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.hutool.core.date.DateBetween;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;

import javax.annotation.Resource;

import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;

import java.util.Optional;
import java.text.DecimalFormat;
import com.swind.fast.modules.app.halali.customer.vo.response.CartPriceVo;

/**
 * @Description: 项目
 * @Author: swind
 * @Date:   2021-11-20
 * @Version: V1.0
 */
@Slf4j
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements ICartService {

    @Autowired
    private IRestaurantMealService restaurantMealService;

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IOrderItemService orderItemService;

    public CartPriceVo calcOrder(List<OrderItem> orderItems) {
        String serviceFee = "1.00";
        String deliveryFee = "5.00";
        String discount = "5.00";
        String voucher = "0.00";

        List<String> itemIds = new ArrayList<String>();
        if (orderItems.size() > 0) {
            itemIds = orderItems.stream().map(OrderItem::getMealId).distinct().collect(Collectors.toList());
        }
        List<RestaurantMeal> meals = restaurantMealService.listByIds(itemIds);

        Double totalPrice = 0.00;
        for (OrderItem pitem : orderItems) {
            Optional<RestaurantMeal> itemOpt = meals.stream().filter(p -> p.getId().equals(pitem.getMealId())).findFirst();

            if (itemOpt.isPresent()) {
                RestaurantMeal meal = itemOpt.get();
                Double mealPrice = oConvertUtils.getDouble(meal.getDiscountPrice(), 0.00);
                totalPrice += mealPrice * oConvertUtils.getInt(pitem.getNum().toString());
                pitem.setRestaurantId(meal.getRestaurantId());
                pitem.setMealId(meal.getId());
                pitem.setPrice(meal.getDiscountPrice());
                pitem.setQuantity(pitem.getNum());
            }
        }

        totalPrice += oConvertUtils.getDouble(serviceFee, 0.00);
        totalPrice += oConvertUtils.getDouble(deliveryFee, 0.00);
        totalPrice -= oConvertUtils.getDouble(discount, 0.00);
        totalPrice -= oConvertUtils.getDouble(voucher, 0.00);

        DecimalFormat df = new DecimalFormat("#.00");
        String formatPrice = df.format(totalPrice);

        // log.info("meal total price: {}", formatPrice);

        CartPriceVo ret = new CartPriceVo();
        ret.setServiceFee(serviceFee);
        ret.setDeliveryFee(deliveryFee);
        ret.setDiscount(discount);
        ret.setVoucher(voucher);
        ret.setTotalPrice(totalPrice);

        Order order = orderService.saveMain(ret, orderItems);
        for (OrderItem pitem : orderItems) {
            pitem.setOrderId(order.getId());
        }
        ret.setOrderId(order.getId());
        // log.info("order items: {}", orderItems);

        orderItemService.saveBatch(orderItems);
        return ret;
    }

    public CartPriceVo calcPrice(OrderItem[] orderItems) {
        String serviceFee = "1.00";
        String deliveryFee = "5.00";
        String discount = "5.00";
        String voucher = "0.00";

        List<String> itemIds = new ArrayList<String>();
        for (OrderItem item : orderItems) {
            // log.info("item id: {}", item.getMealId());
            itemIds.add(item.getMealId());
        }
        // log.info("item ids: {}", itemIds);

        List<RestaurantMeal> meals = restaurantMealService.listByIds(itemIds);
        // log.info("meal list: {}", meals);

        Double totalPrice = 0.00;
        for (OrderItem pitem : orderItems) {
            Optional<RestaurantMeal> itemOpt = meals.stream().filter(p -> p.getId().equals(pitem.getMealId())).findFirst();

            if (itemOpt.isPresent()) {
                RestaurantMeal meal = itemOpt.get();
                log.info("meal: {}", meal);
                // Double mealPrice = oConvertUtils.getDouble(meal.getPrice(), 0.00);
                Double mealPrice = oConvertUtils.getDouble(meal.getDiscountPrice(), 0.00);
                totalPrice += mealPrice * oConvertUtils.getInt(pitem.getNum().toString());
            }
        }

        totalPrice += oConvertUtils.getDouble(serviceFee, 0.00);
        totalPrice += oConvertUtils.getDouble(deliveryFee, 0.00);
        totalPrice -= oConvertUtils.getDouble(discount, 0.00);
        totalPrice -= oConvertUtils.getDouble(voucher, 0.00);

        DecimalFormat df = new DecimalFormat("#.00");
        String formatPrice = df.format(totalPrice);

        log.info("meal total price: {}", formatPrice);

        CartPriceVo ret = new CartPriceVo();
        ret.setServiceFee(serviceFee);
        ret.setDeliveryFee(deliveryFee);
        ret.setDiscount(discount);
        ret.setVoucher(voucher);
        ret.setTotalPrice(totalPrice);

        return ret;
    }

}
